<template>
    <div class="all">
        <!-- <div style="text-align:center;font-size:28px;font-weight:bold;">{{$route.name}}</div> -->
        <div class="search">
            <!-- label-width="85px" -->
            <el-form ref="form" :model="platformSearch" :inline="true" size="mini">
                <el-form-item label="分类名称：">
                    <el-input v-model="platformSearch.name" clearable placeholder="请输入姓名"></el-input>
                </el-form-item>
                <!-- <el-form-item label="联系方式：">
                    <el-input
                        v-model="platformSearch.phone"
                        maxlength="11"
                        clearable
                        placeholder="请输入联系方式"
                    ></el-input>
                </el-form-item>
                <el-form-item label="车牌：">
                    <el-select v-model="platformSearch.image_url" clearable placeholder="请选择车牌">
                        <el-option v-for="item in carname" :key="item.id" :label="item.plate_number" :value="item.id"></el-option>
                    </el-select>
                </el-form-item> -->
                <el-form-item>
                    <el-button type="primary" @click="searchClick(2)">查 询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="btn" v-if="jurisdiction.add_state === 1">
            <el-button size="medium" type="primary" @click="add()">添加</el-button>
        </div>
        <span v-else></span>
        <div class="table">
            <!-- 表格 -->
            <el-table
                :data="platformTable"
                border
                height="560"
                :cell-style="cellStyle"
                :header-cell-style="rowClass"
                @selection-change="allSelectionChange"
            >
                <el-table-column label="ID" width="80px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.id }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="分类名称" width="160px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="图片地址" width="140px">
                    <template slot-scope="scope">
                        <!-- <span>{{ scope.row.image_url }}</span> -->
                        <!-- <img :src="scope.row.image_url" alt=""> -->
                        <div @click="browseStoreUrl(scope.$index, scope.row)">
                            <img :src="scope.row.image_url" style="width:60px;" alt="分类图片" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="220px" v-if="jurisdiction.update_state === 1 || jurisdiction.delete_state === 1">
                    <template slot-scope="scope">
                        <el-button
                            v-if="jurisdiction.update_state === 1"
                            size="small"
                            type="primary"
                            @click="edit(scope.$index, scope.row)"
                        >修改</el-button>
                        <el-button
                            v-if="jurisdiction.delete_state === 1"
                            size="small"
                            type="danger"
                            @click="remove(scope.$index, scope.row)"
                        >删除</el-button>
                        <!-- <el-button
                            size="small"
                            type="warning"
                            @click="approval(scope.$index, scope.row) handleupdate"
                        >审批</el-button> -->
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
                @size-change="pageCount"
                @current-change="currentPageCount"
                :current-page="currentPage"
                :page-sizes="[100, 500,1000,1500, 2000]"
                :page-size="currentCount"
                layout="prev, pager, next, jumper, sizes,total"
                :total="pageResultSize"
            ></el-pagination>
        </div>
        <div>
            <el-dialog center width="700px" :visible.sync="dialogVisible">
                <div style="text-align:center;">
                    <img width="250px" height="auto" :src="dialogImageUrl" alt />
                </div>
            </el-dialog>

            <!-- 修改对话框 -->
            <el-dialog title="修改" width="40%" :visible.sync="editVisible" center @close="closeEdit">
                <el-form :model="editPlatform" ref="editPlatform" :rules="rules">
                    <p class=" tc ff53 mb10">*直接点原本的分类图片，从本地上传新的图片提交即可</p>
                    <el-form-item
                        label="姓名："
                        :label-width="formLabelWidth"
                        prop="name"
                    >
                        <el-input v-model="editPlatform.name" clearable placeholder="请输入分类名称"></el-input>
                    </el-form-item>
                    <el-form-item label="分类图片" :label-width="formLabelWidth" prop="image_url">
                        <el-upload
                            ref="image_url"
                            v-model="editPlatform.image_url"
                            action="/index.php/fileUpload/upload"
                            name="image"
                            :show-file-list="false"
                            list-type="picture-card"
                            :on-preview="coverPreview"
                            :on-success="handleupdate"
                        >
                            <img v-if="editPlatform.image_url" :src="editPlatform.image_url" class="avatar" />
                            <i v-else class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <div slot="footer">
                    <el-button @click="cancelEdit">取 消</el-button>
                    <el-button type="primary" @click="confirmEdit">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 审批对话框 -->
            <el-dialog
                title="添加"
                width="500px"
                :visible.sync="approvalVisible"
                center
                @close="closeAdd"
            >
                <el-form :model="addclassify" ref="addclassify" :rules="rules">
                    <el-form-item
                        label="姓名："
                        :label-width="formLabelWidth"
                        prop="name"
                    >
                        <el-input v-model="addclassify.name" clearable placeholder="请输入分类名称"></el-input>
                    </el-form-item>
                    <el-form-item label="分类图片" :label-width="formLabelWidth" prop="image_url">
                        <el-upload
                            ref="image_url"
                            v-model="addclassify.image_url"
                            action="/index.php/fileUpload/upload"
                            name="image"
                            :show-file-list="false"
                            list-type="picture-card"
                            :on-preview="coverPreview"
                            :on-success="handleadd"
                        >
                            <img v-if="addclassify.image_url" :src="addclassify.image_url" class="avatar" />
                            <i v-else class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <div slot="footer">
                    <el-button @click="cancelAdd">取消</el-button>
                    <el-button type="primary" @click="confirmAdd">提交</el-button>
                </div>
            </el-dialog>
            <!-- 图片预览 -->
            <!-- <el-dialog width="600px" center :visible.sync="dialogVisible">
                <div style="text-align:center;">
                    <img width="250px" height="auto" :src="dialogImageUrl" alt />
                </div>
            </el-dialog> -->
        </div>
    </div>
</template>

<script>
import "../../../static/scss/page.scss";
import utils from "../../util/request";
import {date} from "../../util/time.js"
import {formatDate} from "../../util/time.js"
var that
export default {
    data() {
        return {
            // 分页
            jurisdiction: null,
            currentPage: 1, //初始页面
            currentCount: 100, //当前页面的数据数量
            pageResultSize: 10, //表格数据总数
            screenWidth: window.innerWidth, //可视宽度
            dialogVisible: false, //放大图片的对话框显示
            dialogImageUrl: "", //放大图片的路径
            editVisible: false, //修改对话框
            approvalVisible: false, //审批对话框
            formLabelWidth: "100px", //对话框的label宽度
            carname: null,
            addclassify:{
                name: "",
                image_url: ""
            },
            editPlatform: {
                // 修改对话框
                id:"",
                name: "",
                image_url: ""
            },
            platformSearch: {
                //搜索
                name: "",//姓名
            },
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                }
            },
            rules:{
                name:[
                    {
                        required: true,
                        message: "请输入分类名称",
                        trigger: "blur"
                    }
                ],
                image_url:[
                    {
                        required: true,
                        message: "请选择活动内容图片",
                        trigger: "change"
                    }
                ]
            },
            platformTable: [],
            selectTable: [], //选中要删除的数组
            cartype:[
                {id:1,name:"轿车"},
                {id:2,name:"SUV"},
                {id:3,name:"MPV"},
            ],
            runstate:["等待申请","通过","拒绝"],
            dialogImageUrl: "",
            dialogVisible: false
        };
    },
    filters: {
        
        // 车牌
        plateNumber(id){
            if(that.carname !== null){
                that.carname.forEach(item=>{
                    id === item.id ? id = item.plate_number : "";
                })
            }
            return id
        },
        // 车架号
        chassisNumber(id){
            if(that.carname !== null){
                that.carname.forEach(item=>{
                    id === item.id ? id = item.chassis_number : "";
                })
            }
            return id
        },
        // 车辆类型
        carType(id){
            if(that.carname !== null){
                that.carname.forEach(item=>{
                    id === item.id ? id = that.cartype[item.type-1].name : "";
                })
            }
            return id
        },
        // 添加时间
        createTime(id){
            if(that.carname !== null){
                that.carname.forEach(item=>{
                    id === item.id ? id = date(item.create_time*1000) : "";
                })
            }
            return id
        },
        // 时间戳转换
        formatDate(time){
			var date = new Date(time);
			return formatDate(date, 'yyyy-MM-dd');
		},
		formatDateBehind(time){
			var date = new Date(time);
			return formatDate(date, 'hh:mm:ss')
		}
    },
    methods: {
        goBack: function() {
            // 页头的返回
            this.$router.go(-1);
        },
        browseProveUrl(index, row) {
            this.dialogImageUrl = row.proveUrl;
            this.dialogVisible = true;
        },
        searchClick(num) {
            // 查询
            let data = {page:this.currentPage,list_rows:this.currentCount};
            this.platformSearch.name !== "" ? data.name = this.platformSearch.name : "";
            utils.request("/kind/list","post",JSON.stringify(data)).then(resp=>{
                console.log(resp)
                this.platformTable = resp.data.data.data;
                this.pageResultSize = resp.data.data.total;
                if(num === 2){
                    resp.data.data.total 
                    ? this.$message({
                        message: "找到"+resp.data.data.total+"条运营车辆申请数据",
                        type: "success"
                    }) : this.$message("没有符合条件的数据");
                }else{return}
            }) 
        },
        add(){
            this.approvalVisible = true;
        },
        handleupdate(resp,file,fileList){
            console.log(resp,file,fileList)
            this.editPlatform.image_url = "https://dzylab.dpyun.site/storage/"+resp.data;
            // console.log(this.addclassify)
        },
        handleadd(resp,file,fileList){
            console.log(resp,file,fileList)
            this.addclassify.image_url = "https://dzylab.dpyun.site/storage/"+resp.data;
            // console.log(this.addclassify)
        },
        // 图片预览
        coverPreview(file) {
            // 点击文件列表中已上传的文件时的钩子
            console.log(file);
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        browseStoreUrl(index, row) {
            this.dialogImageUrl = row.image_url;
            this.dialogVisible = true;
        },
        edit(index, row) {
            // 修改
            this.editVisible = true;
            this.editPlatform.index = index;
            this.editPlatform.id = row.id;
            this.editPlatform.name = row.name;
            this.editPlatform.image_url = row.image_url;
            
        },
        confirmEdit() {
            // 确认修改对话框
            this.editVisible = false;
            this.$refs["editPlatform"].validate((valid)=>{
                if(valid){
                    console.log("123")
                    let data = {id:this.editPlatform.id,name:this.editPlatform.name,image_url:this.editPlatform.image_url}
                    utils.request("/kind/update","post",JSON.stringify(data)).then(resp=>{
                        // console.log(resp,data)
                        if(resp.data.code === 200){
                            this.platformTable.splice(this.editPlatform.index,1,data);
                            for(var i in this.editPlatform)this.editPlatform[i] = "";
                            this.$refs.image_url.clearFiles();
                            this.approvalVisible = false;
                            this.$message({
                                message: "修改成功！",
                                type:"success "
                            })
                        }else{return}
                    })
                }else{return}
            })
        },
        cancelEdit() {
            // 取消修改对话框
            this.editVisible = false;
            for(var i in this.editPlatform)this.editPlatform[i] = "";
        },
        closeEdit() {
            // 关闭修改对话框
            this.editVisible = false;
        },
        remove(index, row) {
            // 删除
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                utils.request("/kind/delete","post",JSON.stringify({id:row.id})).then(resp=>{
                    // console.log(resp,data)
                    if(resp.data.code === 200){
                        this.platformTable.splice(index,1)
                        this.$message({
                            message: "删除成功！",
                            type:"success "
                        })
                    }else{return}
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        // approval(index, row) {
        //     // 审批
        //     console.log(index, row);
        //     this.approvalVisible = true;
        //     for(var i in this.addclassify) this.addclassify[i] = row[i];
        //     this.addclassify.index = index;
        //     // this.addclassify.name = row.name;
        //     // this.addclassify.image_url = row.image_url;
        //     // this.addclassify.end_time = row.end_time;
        //     // this.addclassify.platform = row.platform;
        // },
        confirmAdd() {
            // 添加
            this.$refs["addclassify"].validate((valid)=>{
                if(valid){
                    console.log("123")
                    let data = {name:this.addclassify.name,image_url:this.addclassify.image_url}
                    utils.request("/kind/add","post",JSON.stringify(data)).then(resp=>{
                        console.log(resp,data)
                        if(resp.data.code === 200){
                            data.id = resp.data.data;
                            this.platformTable.length < this.currentCount ? this.platformTable.push(data) : "";
                            this.addclassify.name = "";
                            this.addclassify.image_url = "";
                            this.$refs.image_url.clearFiles();
                            this.approvalVisible = false;
                            this.$message({
                                message: "添加成功！",
                                type:"success "
                            })
                        }else{return}
                    })
                }else{return}
            })
        },
        cancelAdd() {
            // 拒绝审批对话框
            this.addclassify.name = "";
            this.addclassify.image_url = "";
            this.approvalVisible = false;
        },
        closeAdd() {
            // 关闭审批对话框
            this.approvalVisible = false;
            this.addclassify.name = "";
            this.addclassify.image_url = "";
        },
        //   表格居中
        cellStyle: function({ row, column, rowIndex, columnIndex }) {
            return "text-align:center;";
        },
        rowClass: function({ row, rowIndex }) {
            return "text-align:center;";
        },
        allSelectionChange: function(val) {
            // 获取选中的内容
            console.log(val);
            this.selectTable = val;
        },
        // 每页数据的数量
        pageCount: function(val) {
            this.currentCount = val;
            this.searchClick(1)
        },
        // 点击的第几页
        currentPageCount: function(val) {
            this.currentPage = val;
            this.searchClick(1)
        }
    },
    created() {
        let Base64 = require("js-base64").Base64;
        let menuarr = JSON.parse(Base64.decode(sessionStorage.getItem("menuarr")))
        for(var i=0;i<menuarr.length;i++){
            // console.log(menuarr[i].link)
            // console.log(menuarr[i])
            if(menuarr[i].link === this.$route.path){
                this.jurisdiction = menuarr[i]
            }else{}
        }
        console.log(this.jurisdiction)
        // console.log(this.$route)
        this.searchClick(1)
    },
    computed: {},
    watch: {},
    beforeCreate() {
        that = this;
    }
};
</script>


<style lang="scss" scoped>
.flex {
    display: flex;
    flex-flow: row wrap;
}
.avatar {
    //图片大小
    width: 100%;
    height: 100%;
    display: block;
}
.store{
    width: calc(100% - 130px);
    padding: 20px;
    background-color: #ffffff;
    box-sizing: border-box;
    border-radius: 5px;
}
</style>
